﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
using AssociationManager;
using Xzen.Images.Provider.Windows;
using Xzen.Images.Provider;

namespace ReImage
{

    public partial class MainDialog : Form
    {
        string currentDirectory = Environment.CurrentDirectory;
        string[] arguments;
        public enum ResizeCondition
        {
            ShowAll = 1,
            HideFolder = 2,
            ShowImageWithFolder =3,
            ShowImageWithoutFolder =4,
            HideImageWithFolder = 5,
            HideImageWithoutFolder = 6
        }
        ResizeCondition currentCondition = ResizeCondition.ShowAll;
        UserSettings settings;
        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.ListView listView1;
        private void MoreInit()
        {
            treeView1 = new TreeView();
            listView1 = new ListView();
            // 
            // treeView1
            // 
            this.treeView1.Location = new System.Drawing.Point(3, 3);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(121, 361);
            this.treeView1.TabIndex = 0;
            this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
            // 
            // listView1
            // 
            this.listView1.Location = new System.Drawing.Point(130, 3);
            this.listView1.Name = "listView1";
            this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
            this.listView1.Size = new System.Drawing.Size(676, 358);
            this.listView1.TabIndex = 1;
            this.listView1.UseCompatibleStateImageBehavior = false;

            this.flowLayoutPanel1.Controls.Add(this.treeView1);
            this.flowLayoutPanel1.Controls.Add(this.listView1);
            // Images

            this.listView1.LargeImageList = imageList1;
            this.listView1.SmallImageList = imageList1;
            this.listView1.SmallImageList = imageList1;

            this.treeView1.ImageList = imageList1;

            listView1.MouseDoubleClick += new MouseEventHandler(listView1_MouseDoubleClick);

        }

        void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = listView1.FocusedItem;
            if (item != null)
            {
                ListTag tag = item.Tag as ListTag;
                if (tag != null)
                {
                    if (tag.TagType == "D")
                    {
                        currentDirectory = tag.FullPath;
                        LoadLocalDirectory(currentDirectory);
                    }
                }
            }
        }
        public MainDialog( string[] args = null)
        {
            arguments = args;
            InitializeComponent();
            settings = new UserSettings();
           
        }
        private void MainDialog_Load(object sender, EventArgs e)
        {
            settings.Merge(string.Empty);
            MoreInit();
            Resize();
            if (arguments != null)
            {
                if (arguments.Length > 0)
                {
                    this.Text = arguments[0];
                }
            }
        }
        private void LoadLocalDirectory(string root)
        {
            DiskReaderWriter writer = new DiskReaderWriter(root);
            var rootFolder = writer.GetRoot();
            List<Folder> folders = writer.GetFolders(rootFolder);
            List<File> files = writer.GetFiles(rootFolder);
            listView1.Items.Clear();
            listView1.SmallImageList = imageList1;
            listView1.LargeImageList = imageList1;
            foreach (var folder in folders)
            {
                ListViewItem folderItem = new ListViewItem(folder.Name,23);
                ListTag tag = new ListTag() { FullPath = folder.FullName, TagType = "D" };
                folderItem.Tag = tag;
                listView1.Items.Add(folderItem);
            }
            foreach (var file in files)
            {
                ListViewItem fileItem = new ListViewItem(string.Format("{0}{1}",file.Name,file.MimeType),20);
                ListTag tag = new ListTag() { FullPath = file.FullName, TagType = "F" };
                fileItem.Tag = tag;
                listView1.Items.Add(fileItem);
            }
        }
        private new void Resize()
        {
            switch (currentCondition)
            {
                case ResizeCondition.ShowAll:
                    ShowFolder();
                    break;
            }
        }
        private void ShowFolder()
        {
            treeView1.Show();
            treeView1.Dock = DockStyle.Top;
            listView1.Dock = DockStyle.Top;
            treeView1.Height = this.flowLayoutPanel1.Height -  settings.GapBetweenControl;
            listView1.Height = this.flowLayoutPanel1.Height - settings.GapBetweenControl;
            treeView1.Width = settings.LeftFolderWidth;
            listView1.Width = this.flowLayoutPanel1.Width - settings.LeftFolderWidth - settings.GapBetweenControl;
            //
            LoadLocalDirectory(currentDirectory);
        }
        private void HideFolder()
        {
            treeView1.Hide();
            listView1.Width = this.flowLayoutPanel1.Width - settings.GapBetweenControl;
        }
        private void ShowImage()
        {           
            listView1.Show();
            if (treeView1.Visible)
            {
                treeView1.Width = settings.LeftFolderWidth;
                listView1.Width = this.flowLayoutPanel1.Width - settings.LeftFolderWidth - settings.GapBetweenControl;
            }
            else
            {
                listView1.Width = this.flowLayoutPanel1.Width - settings.GapBetweenControl;
            }
        }
        private void HideImage()
        {
            listView1.Hide();
            treeView1.Width = this.flowLayoutPanel1.Width - -settings.GapBetweenControl;           
        }
        private void folderViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // MakeChange();
            var folderViewToolStripMenuItem = sender as ToolStripMenuItem;
            if (folderViewToolStripMenuItem.Text == settings.ViewFolder)
            {
                ShowFolder();
                folderViewToolStripMenuItem.Text = settings.HideFolder;
            }
            else
            {
                HideFolder();
                folderViewToolStripMenuItem.Text = settings.ViewFolder;              
            }
        }

        private void imagesViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var imageViewToolStripMenuItem = sender as ToolStripMenuItem;
            if (imageViewToolStripMenuItem.Text == settings.ViewImage)
            {               
                ShowImage();
                imageViewToolStripMenuItem.Text = settings.HideImage;
            }
            else
            {
                HideImage();
                imageViewToolStripMenuItem.Text = settings.ViewImage;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            System.IO.DirectoryInfo dr = new System.IO.DirectoryInfo(currentDirectory);
            if (dr.Parent != null)
            {
                currentDirectory = dr.Parent.FullName;
                LoadLocalDirectory(currentDirectory);
            }
        }

        private void listToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.List;
        }

        private void largeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.LargeIcon;
        }

        private void smallToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.SmallIcon;
        }

        private void titleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.Tile;
        }        
    }
}
